﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    public partial class TEK04
    {
        public TEK04()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VEKKOPF();
            DataContext = MV;
            InitializeComponent();
        }

        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 8)
                return;
            dwData = TBMain.NewRow();
            dwData["EKKMANDANT"] = app.Mandant;
            dwData["EKKAUFKRS"] = ORDERNUM.Substring(1, 2);
            dwData["EKKAUFLFD"] = ORDERNUM.Substring(3, 6);
            SResult = TMV.QueryEKKOPF(dwData, 3);
            MergeData(SResult, true);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";
                WAEHRSL = " ";
                SACHB = "".PadRight(8);
                LSKOND = "";
                KEY = "";
                LIEFNR = "";
                BUKRS = " ";
                AUF = "";
                KURS = "";
                SART = "";
                VSPED = "";
                ZAHLBED = "";
                SACHB = "";
                LIEFBED = "";
                AUFART = "";
            }
            else
            {
                LIEFBED = TMV.EKKKOMMNR.Substring(0, 3);
                SART = TMV.EKKKOMMNR.Substring(3, 3);
                VSPED = TMV.EKKKOMMNR.Substring(6, 3);
                ZAHLBED = TMV.EKKKOMMNR.Substring(9, 3);
                SACHB = TMV.EKKSACHB;
                LIEFNR = TMV.EKKLIEFNR;
                LSKOND = TMV.EKKLIEFNR + TMV.EKKKONDSL;
                BUKRS = TMV.EKKBUCHKRS;
                WAEHRSL = TMV.EKKWAEHRSL;
                AUFART = TMV.EKKAUFART;
                KEY = TMV.EKKAUFKRS + TMV.EKKAUFLFD + "000" + TMV.EKKLIEFNR;
                AUF = "B" + dwMaster["EKKAUFKRS"].ToString() + dwMaster["EKKAUFLFD"].ToString() + "000" + LIEFNR.PadRight(8);
            }
        }
        public override void OnKURSChg(string value)
        {
            if (IsEdit)
                return;
            if (dwMaster == null)
                return;
            base.OnKURSChg(value);
        }
        public void ChgLIEFNRCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                LIEFNR = "";
                IsNew = true;
            }
            if (dwMaster == null)
                return;
            if (LIEFNR == "")
                return;
            dwMaster["EKKLIEFNR"] = LIEFNR;
            TMV.UpdateEKKOPF(dwMaster, 3);
            IsNew = false;
            LIEFNR = "";
            MessageBox.Show("请重新执行查询");

        }
        public override void OnKONDSLChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (value.Length == 0)
                return;
            TMV.EKKKONDSL = value.PadRight(4);
            LSKOND = TMV.EKKLIEFNR + TMV.EKKKONDSL;
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (dwMaster == null)
                return;
            if (KURS.Trim() == "0" || KURS.Trim() == "")
            {
                MessageBox.Show("请修改汇率！");
            }
            TMV.UpdateEKKOPF(dwMaster, 0, app.company);
            base.OnSave();
        }

        public override void OnLIEFBEDChg(string lb)
        {

            if (!IsEdit)
                return;
            TMV.EKKKOMMNR = (LIEFBED.Trim().PadRight(3) + SART.Trim().PadRight(3) + VSPED.Trim().PadRight(3) + ZAHLBED.Trim().PadRight(3)).PadRight(20);
            base.OnLIEFBEDChg(lb);
        }
        public override void OnSARTChg(string sa)
        {
            if (!IsEdit)
                return;
            TMV.EKKKOMMNR = (LIEFBED.Trim().PadRight(3) + SART.Trim().PadRight(3) + VSPED.Trim().PadRight(3) + ZAHLBED.Trim().PadRight(3)).PadRight(20);
            base.OnSARTChg(sa);
        }
        public override void OnVSPEDChg(string vs)
        {
            if (!IsEdit)
                return;
            TMV.EKKKOMMNR = (LIEFBED.Trim().PadRight(3) + SART.Trim().PadRight(3) + VSPED.Trim().PadRight(3) + ZAHLBED.Trim().PadRight(3)).PadRight(20);
            base.OnVSPEDChg(vs);
        }
        public override void OnZAHLBEDChg(string lb)
        {
            if (!IsEdit)
                return;
            TMV.EKKKOMMNR = (LIEFBED.Trim().PadRight(3) + SART.Trim().PadRight(3) + VSPED.Trim().PadRight(3) + ZAHLBED.Trim().PadRight(3)).PadRight(20);
            base.OnZAHLBEDChg(lb);
        }
    }
}
